angular.module('imsApp').controller('adDataDayStatisticsController', ['$scope', '$state', 'AdDataManager', function ($scope, $state, AdDataManager) {

    var xAxis = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
    var yTodayShowAxis = [];
    var yTodayClickAxis = [];
    var yYestodayShowAxis = [];
    var yYestodayClickAxis = [];
    var yDay7BeforeShowAxis = [];
    var yDay7BeforeClickAxis = [];
    var yDay30BeforeShowAxis = [];
    var yDay30BeforeClickAxis = [];
    $scope.mode = 'show';
    $scope.search={};

    AdDataManager.adDataDay().then(function (result) {
        $scope.topData=result.data;
    })

    $scope.rangeDate = {
        startDate:moment().startOf('day').add(-8, 'days').format('X'),
        endDate: moment().endOf('day').add(0, 'days').format('X'),
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.search.startDay = moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD");
            $scope.search.endDay = moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD");
            AdDataManager.adDataDay($scope.search).then(function (result) {
                $scope.topData=result.data;
            })

        }
    };


    AdDataManager.adDataRt().then(function (result) {
        console.log(result)

        $scope.dataCenter = result.data;
        if ($scope.dataCenter.hasOwnProperty('todayShowNumList')) {
            $scope.dataCenter.todayShowNumList.forEach(function (item) {
                yTodayShowAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('yestodayShowNumList')) {
            $scope.dataCenter.yestodayShowNumList.forEach(function (item) {
                yYestodayShowAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('before7ShowNumList')) {
            $scope.dataCenter.before7ShowNumList.forEach(function (item) {
                yDay7BeforeShowAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('before30ShowNumList')) {
            $scope.dataCenter.before30ShowNumList.forEach(function (item) {
                yDay30BeforeShowAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('todayClickNumList')) {
            $scope.dataCenter.todayClickNumList.forEach(function (item) {
                yTodayClickAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('yestodayClickNumList')) {
            $scope.dataCenter.yestodayClickNumList.forEach(function (item) {
                yYestodayClickAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('before7ClickNumList')) {
            $scope.dataCenter.before7ClickNumList.forEach(function (item) {
                yDay7BeforeClickAxis.push(item.num)
            })
        }
        if ($scope.dataCenter.hasOwnProperty('before30ClickNumList')) {
            $scope.dataCenter.before30ClickNumList.forEach(function (item) {
                yDay30BeforeClickAxis.push(item.num)
            })
        }

        setDataShow();

    })

    function setDataShow() {
        $scope.chartOptShow = {

            tooltip: {
                trigger: 'axis'
            },
            legend: {
                bottom: '0',
                padding:[10,0,0,0],

                data: ['今日展示', '昨日展示', '7天前展示', '30天前展示']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: xAxis
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name:'今日展示' ,
                    type: 'line',
                    stack: '总量',
                    data:  yTodayShowAxis
                },
                {
                    name:  '昨日展示' ,
                    type: 'line',
                    stack: '总量',
                    data:yYestodayShowAxis
                },
                {
                    name:  '7天前展示',
                    type: 'line',
                    stack: '总量',
                    data:yDay7BeforeShowAxis
                },
                {
                    name: '30天前展示' ,
                    type: 'line',
                    stack: '总量',
                    data:  yDay30BeforeShowAxis
                }]
        };
    }
    function setDataClick() {
        $scope.chartOptClick = {

            tooltip: {
                trigger: 'axis'
            },
            legend: {
                bottom: '0',
                padding:[10,0,0,0],
                data: ['今日点击', '昨日点击', '7天前点击', '30天前点击']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: xAxis
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: '今日点击',
                    type: 'line',
                    stack: '总量',
                    data: yTodayClickAxis
                },
                {
                    name: '昨日点击',
                    type: 'line',
                    stack: '总量',
                    data: yYestodayClickAxis
                },
                {
                    name: '7天前点击',
                    type: 'line',
                    stack: '总量',
                    data: yDay7BeforeClickAxis
                },
                {
                    name: '30天前点击',
                    type: 'line',
                    stack: '总量',
                    data: yDay30BeforeClickAxis
                }]
        };
    }


    $scope.filterClick = function (mode) {
        $scope.mode = mode;
        if (mode == 'show') {
            setDataShow();
        } else {
            setDataClick();
        }
    }
    function doFileExport(inName, inStr) {

        var xlsWin = null;

        if (!!document.all("glbHideFrm")) {

            xlsWin = glbHideFrm;

        } else {

            var width = 6;

            var height = 4;

            var openPara = "left=" + (window.screen.width / 2 - width / 2)

                + ",top=" + (window.screen.height / 2 - height / 2)

                + ",scrollbars=no,width=" + width + ",height=" + height;

            xlsWin = window.open("", "_blank", openPara);

        }

        xlsWin.document.write(inStr);

        xlsWin.document.close();

        xlsWin.document.execCommand('Saveas', true, inName);

        xlsWin.close();

    }
    //导出excel

    function getExplorer() {
        var explorer = window.navigator.userAgent;
        //ie
        if (explorer.indexOf("MSIE") >= 0) {
            return 'ie';
        }
        //firefox
        else if (explorer.indexOf("Firefox") >= 0) {
            return 'Firefox';
        }
        //Chrome
        else if (explorer.indexOf("Chrome") >= 0) {
            return 'Chrome';
        }
        //Opera
        else if (explorer.indexOf("Opera") >= 0) {
            return 'Opera';
        }
        //Safari
        else if (explorer.indexOf("Safari") >= 0) {
            return 'Safari';
        } else {
            return 'unknown';
        }
    }
    $scope.method1 =function(tableid) {//整个表格拷贝到EXCEL中
        if (getExplorer() == 'ie' || getExplorer() == "unknown") {
            this.getXlsFromTbl('targetTable', null);
        } else {
            tableToExcel(tableid)
        }
    }
    var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,', template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>', base64 = function(
            s) {
            return window.btoa(unescape(encodeURIComponent(s)))
        }, format = function(s, c) {
            return s.replace(/{(\w+)}/g, function(m, p) {
                return c[p];
            })
        }
        return function(table, name) {
            if (!table.nodeType)
                table = document.getElementById(table)
            var ctx = {
                worksheet : name || 'Worksheet',
                table : table.innerHTML
            }
            var excelData = uri + base64(format(template, ctx));
            var downNode = document.getElementById('download');
            downNode.setAttribute("href",excelData);
            downNode.setAttribute("download",getExcelFileName());
        }
    })()

    /*----------------------------------*/

    function getXlsFromTbl(inTblId, inWindow) {

        try {

            var allStr = "";

            var curStr = "";

            if (inTblId != null && inTblId != "" && inTblId != "null") {

                curStr = getTblData(inTblId, inWindow);

            }

            if (curStr != null) {

                allStr += curStr;

            } else {

                alert("你要导出的表不存在！");

                return;

            }

            var fileName = getExcelFileName();

            doFileExport(fileName, allStr);

        } catch (e) {

            alert("导出发生异常:" + e.name + "->" + e.description + "!");

        }

    }

    function getTblData(inTbl, inWindow) {

        var rows = 0;

        var tblDocument = document;

        if (!!inWindow && inWindow != "") {

            if (!document.all(inWindow)) {

                return null;

            } else {

                tblDocument = eval(inWindow).document;

            }

        }

        var curTbl = tblDocument.getElementById(inTbl);

        var outStr = "";

        if (curTbl != null) {

            for (var j = 0; j < curTbl.rows.length; j++) {

                for (var i = 0; i < curTbl.rows[j].cells.length; i++) {

                    if (i == 0 && rows > 0) {

                        outStr += " /t";

                        rows -= 1;

                    }

                    outStr += curTbl.rows[j].cells[i].innerText + "/t";

                    if (curTbl.rows[j].cells[i].colSpan > 1) {

                        for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {

                            outStr += " /t";

                        }

                    }

                    if (i == 0) {

                        if (rows == 0
                            && curTbl.rows[j].cells[i].rowSpan > 1) {

                            rows = curTbl.rows[j].cells[i].rowSpan - 1;

                        }

                    }

                }

                outStr += "/r/n";

            }

        }

        else {

            outStr = null;

            alert(inTbl + "不存在!");

        }

        return outStr;

    }

    function getExcelFileName() {

        var d = new Date();

        var curYear = d.getFullYear();

        var curMonth = "" + (d.getMonth() + 1);

        var curDate = "" + d.getDate();

        var curHour = "" + d.getHours();

        var curMinute = "" + d.getMinutes();

        var curSecond = "" + d.getSeconds();

        if (curMonth.length == 1) {

            curMonth = "0" + curMonth;

        }

        if (curDate.length == 1) {

            curDate = "0" + curDate;

        }

        if (curHour.length == 1) {

            curHour = "0" + curHour;

        }

        if (curMinute.length == 1) {

            curMinute = "0" + curMinute;

        }

        if (curSecond.length == 1) {

            curSecond = "0" + curSecond;

        }

        var fileName = "数据统计" + "_" + curYear + curMonth + curDate + "_"

            + curHour + curMinute + curSecond + ".xls";

        return fileName;

    }





}])

